package com.rainsoft.center.isec.stream.library.dao;

import com.rainsoft.center.isec.common.utils.JDBCHelper;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;

import java.io.Serializable;
import java.util.List;

/**
 * @Name com.rainsoft.center.isec.stream.library.dao.LibDao
 * @Description
 * @Author Elwyn
 * @Version 2017/11/29
 * @Copyright 上海云辰信息科技有限公司
 **/
public class LibDao implements Serializable {
	private JDBCHelper jdbcHelper = JDBCHelper.getInstance();

	public int[] saveToOracle(Dataset<Row> rowJavaRDD, String sql) {
		List<Object[]> jdbcArgs = rowJavaRDD.javaRDD().map((Function<Row, Object[]>) v1 -> {
			int size = v1.size();
			Object[] objects = new Object[size];
			for (int i = 0; i < size; i++) {
				Object o = v1.get(i);
				objects[i] = o;
			}
			return objects;
		}).collect();
		return batchUpdate(sql, jdbcArgs);
	}


	public int[] batchUpdate(String sql, List<Object[]> jdbcArgs) {
		return jdbcHelper.executeBatch(sql, jdbcArgs);
	}
}
